<template>
  <div class="top-nav-container">
    <div class="nav-decoration"></div>
    
    <el-menu
      :default-active="activeMenu"
      mode="horizontal"
      class="top-menu"
      @select="handleSelect"
    >
      <template v-for="(item, index) in topMenus">
        <el-menu-item 
          :index="item.path" 
          :key="index" 
          v-if="index < visibleNumber"
          class="top-menu-item"
        >
          <svg-icon
            v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
            :icon-class="item.meta.icon"
            class="menu-icon"
          />
          <span class="menu-title">{{ item.meta.title }}</span>
        </el-menu-item>
      </template>

      <!-- 顶部菜单超出数量折叠 -->
      <el-submenu 
        index="more" 
        :key="visibleNumber" 
        v-if="topMenus.length > visibleNumber"
        class="more-menu"
      >
        <template slot="title">
          <i class="el-icon-more menu-icon"></i>
          <span class="menu-title">更多菜单</span>
        </template>
        
        <template v-for="(item, index) in topMenus">
          <el-menu-item
            :index="item.path"
            :key="index"
            v-if="index >= visibleNumber"
            class="submenu-item"
          >
            <svg-icon
              v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
              :icon-class="item.meta.icon"
              class="menu-icon"
            />
            <span class="menu-title">{{ item.meta.title }}</span>
          </el-menu-item>
        </template>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
import { constantRoutes } from "@/router";
import { isHttp } from "@/utils/validate";

// 隐藏侧边栏路由
const hideList = ['/index', '/user/profile'];

export default {
  data() {
    return {
      // 顶部栏初始数
      visibleNumber: 5,
      // 当前激活菜单的 index
      currentIndex: undefined
    };
  },
  computed: {
    theme() {
      return this.$store.state.settings.theme;
    },
    // 顶部显示菜单
    topMenus() {
      let topMenus = [];
      this.routers.map((menu) => {
        if (menu.hidden !== true) {
          // 兼容顶部栏一级菜单内部跳转
          if (menu.path === "/") {
            topMenus.push(menu.children[0]);
          } else {
            topMenus.push(menu);
          }
        }
      });
      return topMenus;
    },
    // 所有的路由信息
    routers() {
      return this.$store.state.permission.topbarRouters;
    },
    // 设置子路由
    childrenMenus() {
      var childrenMenus = [];
      this.routers.map((router) => {
        for (var item in router.children) {
          if (router.children[item].parentPath === undefined) {
            if(router.path === "/") {
              router.children[item].path = "/" + router.children[item].path;
            } else {
              if(!isHttp(router.children[item].path)) {
                router.children[item].path = router.path + "/" + router.children[item].path;
              }
            }
            router.children[item].parentPath = router.path;
          }
          childrenMenus.push(router.children[item]);
        }
      });
      return constantRoutes.concat(childrenMenus);
    },
    // 默认激活的菜单
    activeMenu() {
      const path = this.$route.path;
      let activePath = path;
      if (path !== undefined && path.lastIndexOf("/") > 0 && hideList.indexOf(path) === -1) {
        const tmpPath = path.substring(1, path.length);
        if (!this.$route.meta.link) {
          activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
          this.$store.dispatch('app/toggleSideBarHide', false);
        }
      } else if(!this.$route.children) {
        activePath = path;
        this.$store.dispatch('app/toggleSideBarHide', true);
      }
      this.activeRoutes(activePath);
      return activePath;
    },
  },
  beforeMount() {
    window.addEventListener('resize', this.setVisibleNumber)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setVisibleNumber)
  },
  mounted() {
    this.setVisibleNumber();
  },
  methods: {
    // 根据宽度计算设置显示栏数
    setVisibleNumber() {
      const width = document.body.getBoundingClientRect().width / 3;
      this.visibleNumber = parseInt(width / 85);
    },
    // 菜单选择事件
    handleSelect(key, keyPath) {
      this.currentIndex = key;
      const route = this.routers.find(item => item.path === key);
      if (isHttp(key)) {
        // http(s):// 路径新窗口打开
        window.open(key, "_blank");
      } else if (!route || !route.children) {
        // 没有子路由路径内部打开
        const routeMenu = this.childrenMenus.find(item => item.path === key);
        if (routeMenu && routeMenu.query) {
          let query = JSON.parse(routeMenu.query);
          this.$router.push({ path: key, query: query });
        } else {
          this.$router.push({ path: key });
        }
        this.$store.dispatch('app/toggleSideBarHide', true);
      } else {
        // 显示左侧联动菜单
        this.activeRoutes(key);
        this.$store.dispatch('app/toggleSideBarHide', false);
      }
    },
    // 当前激活的路由
    activeRoutes(key) {
      var routes = [];
      if (this.childrenMenus && this.childrenMenus.length > 0) {
        this.childrenMenus.map((item) => {
          if (key == item.parentPath || (key == "index" && "" == item.path)) {
            routes.push(item);
          }
        });
      }
      if(routes.length > 0) {
        this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
      } else {
        this.$store.dispatch('app/toggleSideBarHide', true);
      }
    }
  },
};
</script>

<style lang="scss">
.top-nav-container {
  position: relative;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(107, 126, 151, 0.08);
  
  .nav-decoration {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(to right, #8ba1bc, #6b7e97);
    z-index: 1;
  }
  
  .top-menu {
    height: 50px;
    line-height: 50px;
    border-bottom: none;
    background-color: transparent;
    display: flex;
    align-items: center;
    padding: 0 15px;
    
    .top-menu-item, 
    .more-menu .el-submenu__title {
      height: 50px !important;
      line-height: 50px !important;
      color: #6b7e97 !important;
      padding: 0 16px !important;
      margin: 0 5px !important;
      border-bottom: 2px solid transparent !important;
      position: relative;
      font-size: 14px;
      transition: all 0.3s;
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%) scaleX(0);
        width: 100%;
        height: 2px;
        background: linear-gradient(to right, #8ba1bc, #6b7e97);
        transition: transform 0.3s ease;
        border-radius: 2px 2px 0 0;
      }
      
      &:hover {
        color: #4a5568 !important;
        background-color: rgba(139, 161, 188, 0.05) !important;
        
        &::after {
          transform: translateX(-50%) scaleX(0.6);
        }
        
        .menu-icon {
          color: #8ba1bc !important;
        }
      }
      
      &.is-active {
        color: #4a5568 !important;
        font-weight: 600;
        
        &::after {
          transform: translateX(-50%) scaleX(1);
        }
        
        .menu-icon {
          color: #8ba1bc !important;
        }
      }
      
      .menu-icon {
        margin-right: 5px;
        color: #a0aec0 !important;
        transition: color 0.3s;
      }
      
      .menu-title {
        display: inline-block;
        transition: transform 0.3s;
      }
    }
    
    .more-menu {
      margin-left: auto;
      
      .el-submenu__icon-arrow {
        color: #a0aec0;
        transition: transform 0.3s;
      }
      
      &:hover {
        .el-submenu__icon-arrow {
          transform: rotate(180deg);
          color: #8ba1bc;
        }
      }
    }
  }
  
  // 下拉菜单样式
  .el-menu--popup {
    padding: 6px 0 !important;
    border-radius: 4px !important;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08) !important;
    
    .submenu-item {
      height: 40px !important;
      line-height: 40px !important;
      color: #6b7e97 !important;
      padding: 0 15px !important;
      
      &:hover {
        background-color: rgba(139, 161, 188, 0.08) !important;
        color: #4a5568 !important;
        
        .menu-icon {
          color: #8ba1bc !important;
        }
      }
      
      &.is-active {
        color: #4a5568 !important;
        background-color: rgba(139, 161, 188, 0.1) !important;
        font-weight: 600;
        
        .menu-icon {
          color: #8ba1bc !important;
        }
      }
      
      .menu-icon {
        margin-right: 5px;
        color: #a0aec0 !important;
        transition: color 0.3s;
      }
    }
  }
}
</style>
